/**
 * Created with IntelliJ IDEA.
 * User: zhaoming
 * Date: 15-8-18
 * Time: 上午11:45
 * To change this template use File | Settings | File Templates.
 */

$(function(){

    loadChangePwdDialog();
    $("#changePwd").click(function(){
        openChangePwdWindow();
    })

    $("#logout").click(function(){
        window.location.href = "/logout";
    });

    //var _menus = [];
    //jQuery.ajax({
    //    type:"POST",
    //    dataType:'json',
    //    async : false,
    //    url:"/menu",
    //    success:function (data) {
    //        _menus = data;
    //    },
    //    error : function(data){
    //        Commons.showError("加载菜单资源失败");
    //    }
    //});

    var _menus = [
        {"id":"1", "code":"basic", "name":"基础信息", "icon":"th-large",
            "menus":[
                {"id":"1","code":"dataTypeMapping","name":"数据类型映射","url":"/web/dataTypeMapping/index"},
                {"id":"2","code":"template","name":"模板管理","url":"/web/template/index"}
            ]
        },
        {"id":"2","code":"analyze","name":"源码生成","icon":"file-code-o",
            "menus":[
                {"id":"1","code":"mysqlGenerator","name":"MySQL元数据生成","icon":"","url":"/web/mysqlGenerator/index"}
            ]
        }
    ];
    addNav(_menus);
});

/**
 * 添加左侧菜单
 * @param data
 */
function addNav(data) {
    var menuList = '';
    menuList += '<li id="homePage">';
    menuList += '<a href="/index">';
    menuList += '<i class="menu-icon fa fa-home"></i>';
    menuList += '<span class="menu-text"> 首页 </span>';
    menuList += '</a>';
    menuList += '</li>';
    $.each(data, function(i, menu) {
        var firstMenu = menu.menus;
        if(firstMenu == null || firstMenu.length == 0){
            menuList += '<li id="'+menu.code+'">';
            menuList += '<a href="'+(menu.url == "" ? "#" : menu.url)+'">';
            menuList += '<i class="menu-icon fa fa-'+menu.icon+'"></i>';
            menuList += '<span class="menu-text"> '+menu.name+' </span>';
            menuList += '</a>';
            menuList += '</li>';
        }else{
            menuList += '<li id="'+menu.code+'">';
            menuList += '<a href="#" class="dropdown-toggle">';
            menuList += '<i class="menu-icon fa fa-'+menu.icon+'"></i>';
            menuList += '<span class="menu-text"> '+menu.name+' </span>';
            menuList += '<b class="arrow fa fa-angle-down"></b>';
            menuList += '</a>';
            menuList += '<ul class="submenu">';
            $.each(firstMenu, function(i, menu) {
                var secondMenu = menu.menus;
                if(secondMenu == null || secondMenu.length == 0){
                    menuList += '<li id="'+menu.code+'">';
                    menuList += '<a href="'+(menu.url == "" ? "#" : menu.url)+'">';
                    menuList += '<i class="menu-icon fa fa-caret-right"></i>';
                    menuList += menu.name;
                    menuList += '</a>';
                    menuList += '</li>';
                }else{
                    menuList += '<li id="'+menu.code+'">';
                    menuList += '<a href="#" class="dropdown-toggle">';
                    menuList += '<i class="menu-icon fa fa-caret-right"></i>';
                    menuList += '<span class="menu-text"> '+menu.name+' </span>';
                    menuList += '<b class="arrow fa fa-angle-down"></b>';
                    menuList += '</a>';
                    menuList += '<ul class="submenu">';
                    $.each(secondMenu, function(i, menu) {
                        menuList += '<li id="'+menu.code+'">';
                        menuList += '<a href="'+(menu.url == "" ? "#" : menu.url)+'">';
                        menuList += '<i class="menu-icon fa fa-'+menu.icon+' green"></i>';
                        menuList += menu.name;
                        menuList += '</a>';
                        menuList += '</li>';
                    });
                    menuList += '</ul>';
                    menuList += '</li>';
                }
            });
            menuList += '</ul>';
            menuList += '</li>';
        }

    });
    $('#menus').html(menuList);
}

/**
 * 初始化密码变更
 */
function loadChangePwdDialog(){

    $("#changePwdDialog").dialog({
        autoOpen: false,
        resizable: false,
        modal: true,
        width: 650,
        height: 310,
        title: "密码变更",
        buttons: [
            {
                text: "保存",
                "class" : "button button-primary button-rounded button-default",
                click: function() {
                    savePwd();
                }
            },
            {
                text: "取消",
                "class" : "button button-action button-rounded button-default",
                click: function() {
                    $( this ).dialog( "close" );
                }
            }
        ]
    });
}

/**
 * 打开分配角色窗口
 * @param userCode 用户编码
 */
function openChangePwdWindow(){
    $("#changePwdForm")[0].reset();
    $("#changePwdDialog").dialog("open");
}

/**
 * 保存密码
 */
function savePwd(){
    if($('#changePwdForm').trigger("validate").isValid()){
        jQuery.ajax({
            type:"POST",
            dataType:'json',
            url: "/web/userInfo/changePassword",
            data:{
                oldPwd: $('#oldPwd').val(),
                newPwd: $('#newPwd').val()
            },
            success:function (data) {
                $("#changePwdDialog").dialog("close");
                Commons.showSuccess("密码变更成功!");
            },
            error : function(data){
                Commons.showError(data.responseText);
            }
        });
    }
}

/**
 * 初始化租户信息
 * @param key
 */
function initTenantInfo(key){
    var element = $("#"+key);
    jQuery.ajax({
        type:"POST",
        dataType:'json',
        async : false,
        url: "/web/userInfo/getCurrentUserInfo",
        success:function (data) {
            if(data.tenantId != null && data.tenantId != ""){
                element.select2("val", data.tenantId);
                element.attr("disabled", true);
            }else{
                element.attr("disabled", false);
            }
        },
        error : function(data){
            Commons.showError(data.responseText);
        }
    });
}

